package com.eyolo.grpcserver.server.mq;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.util.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import scwrapper.Sc;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author fanhailong
 * @date 2021/11/29 15:30
 **/
@Slf4j
@Component
public class RocketMqSender {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Value("${rocketmq.producer.topicSCMint}")
    private String topicSCMint;

    @Value("${rocketmq.producer.topicSCBurn}")
    private String topicSCBurn;

    public void sentMintResultMsg(Sc.OrderInfo orderInfo) {
        try {
            String orderInfoJson = JsonFormat.printer().print(orderInfo);
            SendResult sendResult = rocketMQTemplate.syncSend(topicSCMint, orderInfoJson);
            log.info("铸币结果发送 sentMintResultMsg orderInfo = {}, result = {}", orderInfoJson, sendResult);
        } catch (InvalidProtocolBufferException e) {
            log.error("铸币结果发送异常" ,e);
        }
    }

    public void sentBurnResultMsg(Sc.OrderInfo orderInfo) {
        try {
            String orderInfoJson = JsonFormat.printer().print(orderInfo);
            SendResult sendResult = rocketMQTemplate.syncSend(topicSCBurn, orderInfoJson);
            log.info("销币结果发送 sentMintResultMsg orderInfo = {}, result = {}", orderInfoJson, sendResult);
        } catch (InvalidProtocolBufferException e) {
            log.error("铸币结果发送异常" ,e);
        }
    }
}
